package com.hjx.windowtool.file;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.hjx.windowtool.tablemodel.TableModel;

import lombok.extern.slf4j.Slf4j;

import javax.swing.JProgressBar;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.UIManager;

@Slf4j
public class FileListApp {

	private JFrame frame;
	private JTable table;
	private JTextField textField;
	
	ConcurrentHashMap<String, AtomicInteger> map = new ConcurrentHashMap<>();
	ConcurrentHashMap<String, List<FileTypeInfo>> mapFiles = new ConcurrentHashMap<>();
	ExecutorService executorService = Executors.newFixedThreadPool(100);
	private AtomicInteger runSize=new AtomicInteger();
	private AtomicInteger totalSize=new AtomicInteger();
	private JProgressBar progressBar;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					FileListApp window = new FileListApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public FileListApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setResizeWeight(0.1);
		frame.getContentPane().add(splitPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		
		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable() {
					public void run() {
						totalSize.set(0);
						runSize.set(0);
						map.clear();
						searchDir(new File(textField.getText()));
						while(totalSize.get()>runSize.get()) {
							try {
								Thread.sleep(100);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						log.info("运行完成");
						System.out.println(map);
						AtomicInteger i=new AtomicInteger();
						map.values().forEach(e->i.addAndGet(e.get()));
						System.out.println(i.get());
					}
				}).start();
			}
		});
		
		textField = new JTextField();
		panel.add(textField);
		textField.setColumns(30);
		panel.add(button);
		
		JScrollPane scrollPane = new JScrollPane();
		splitPane.setRightComponent(scrollPane);
		
		table = new JTable(new TableModel(new String[] {"type","size"}, new String[] {"1","2"}));
		scrollPane.setViewportView(table);
		
		progressBar = new JProgressBar();
		frame.getContentPane().add(progressBar, BorderLayout.SOUTH);
	}

	protected void searchDir(File file) {
		Runnable runnable = new Runnable() {
			public void run() {
				try {
					log.info("处理文件：{}-{}-{}",totalSize.get(),runSize.get(),file);
					if (file.isDirectory()) {
						for (File f : file.listFiles()) {
							searchDir(f);
						}
					}else {
						String key="其他";
						if (file.getName().lastIndexOf('.')>-1) {
							key=file.getName().substring(file.getName().lastIndexOf('.')+1);
						}
						AtomicInteger integer = map.getOrDefault(key, new AtomicInteger(0));
						integer.addAndGet(1);
						map.put(key, integer);
						FileTypeInfo info = new FileTypeInfo();
						info.setType(key);
						info.setFile(file);
						List<FileTypeInfo> list = mapFiles.getOrDefault(key, new ArrayList<>());
						list.add(info);
						mapFiles.put(key, list);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}finally {
					runSize.addAndGet(1);
					progressBar.setValue(runSize.get()%100);
					log.info("处理文件完成：{}-{}-{}",totalSize.get(),runSize.get(),file);
				}
			}
		};
		totalSize.addAndGet(1);
		executorService.submit(runnable);
	}

}
